package cn.iocoder.yudao.module.promotion.controller.admin.quickcombinationactivity;

import cn.iocoder.yudao.module.product.api.spu.ProductSpuApi;
import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO;
import cn.iocoder.yudao.module.promotion.controller.admin.quickcombinationactivity.vo.product.QuickCombinationProductRespVO;
import cn.iocoder.yudao.module.promotion.controller.admin.quickcombinationactivity.vo.record.QuickCombinationRecordRespVO;
import cn.iocoder.yudao.module.promotion.convert.quickcombinationactivity.QuickCombinationProductConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.combination.CombinationActivityDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.combination.CombinationProductDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.quickcombinationactivity.QuickCombinationProductDO;
import cn.iocoder.yudao.module.promotion.dal.mysql.quickcombinationactivity.QuickCombinationProductMapper;
import cn.iocoder.yudao.module.promotion.service.combination.CombinationActivityService;
import cn.iocoder.yudao.module.promotion.service.combination.CombinationRecordService;
import cn.iocoder.yudao.module.promotion.service.quickcombinationactivity.QuickCombinationRecordService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.promotion.controller.admin.quickcombinationactivity.vo.*;
import cn.iocoder.yudao.module.promotion.dal.dataobject.quickcombinationactivity.QuickCombinationActivityDO;
import cn.iocoder.yudao.module.promotion.convert.quickcombinationactivity.QuickCombinationActivityConvert;
import cn.iocoder.yudao.module.promotion.service.quickcombinationactivity.QuickCombinationActivityService;

@Tag(name = "管理后台 - 促销活动")
@RestController
@RequestMapping("/promotion/quick-combination-activity")
@Validated
public class QuickCombinationActivityController {

    @Resource
    private QuickCombinationActivityService quickCombinationActivityService;

    @Resource
    private QuickCombinationProductMapper quickCombinationProductMapper;

    @Resource
    private QuickCombinationRecordService quickCombinationRecordService;

    @Resource
    private ProductSpuApi productSpuApi;

    @PostMapping("/create")
    @Operation(summary = "创建进货活动")
    @PreAuthorize("@ss.hasPermission('promotion:quick-combination-activity:create')")
    public CommonResult<Long> createQuickCombinationActivity(@Valid @RequestBody QuickCombinationActivityCreateReqVO createReqVO) {
        return success(quickCombinationActivityService.createQuickCombinationActivity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新进货活动")
    @PreAuthorize("@ss.hasPermission('promotion:quick-combination-activity:update')")
    public CommonResult<Boolean> updateQuickCombinationActivity(@Valid @RequestBody QuickCombinationActivityUpdateReqVO updateReqVO) {
        quickCombinationActivityService.updateQuickCombinationActivity(updateReqVO);
        return success(true);
    }

    @PutMapping("/close")
    @Operation(summary = "关闭拼团活动")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('promotion:combination-activity:close')")
    public CommonResult<Boolean> closeSeckillActivity(@RequestParam("id") Long id) {
        quickCombinationActivityService.closeCombinationActivityById(id);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除进货活动")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('promotion:quick-combination-activity:delete')")
    public CommonResult<Boolean> deleteQuickCombinationActivity(@RequestParam("id") Long id) {
        quickCombinationActivityService.deleteQuickCombinationActivity(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得进货活动")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('promotion:quick-combination-activity:query')")
    public CommonResult<QuickCombinationActivityRespVO> getQuickCombinationActivity(@RequestParam("id") Long id) {
        QuickCombinationActivityDO quickCombinationActivity = quickCombinationActivityService.getQuickCombinationActivity(id);

        List<QuickCombinationProductDO> products = quickCombinationProductMapper.selectListByActivityId(id);
        List<QuickCombinationProductRespVO> productList = QuickCombinationProductConvert.INSTANCE.convertList(products);
        QuickCombinationActivityRespVO respVO = QuickCombinationActivityConvert.INSTANCE.convert(quickCombinationActivity);
        respVO.setProducts(productList);

        return success(respVO);
    }

    @GetMapping("/list")
    @Operation(summary = "获得进货活动列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('promotion:quick-combination-activity:query')")
    public CommonResult<List<QuickCombinationActivityRespVO>> getQuickCombinationActivityList(@RequestParam("ids") Collection<Long> ids) {
        List<QuickCombinationActivityDO> list = quickCombinationActivityService.getQuickCombinationActivityList(ids);
        return success(QuickCombinationActivityConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得进货活动分页")
    @PreAuthorize("@ss.hasPermission('promotion:quick-combination-activity:query')")
    public CommonResult<PageResult<QuickCombinationActivityRespVO>> getQuickCombinationActivityPage(@Valid QuickCombinationActivityPageReqVO pageVO) {
        PageResult<QuickCombinationActivityDO> pageResult = quickCombinationActivityService.getQuickCombinationActivityPage(pageVO);

//        // 拼接数据
//        List<QuickCombinationProductDO> products = quickCombinationActivityService.getCombinationProductListByActivityIds(
//                convertSet(pageResult.getList(), QuickCombinationActivityDO::getId));

        List<ProductSpuRespDTO> spus = productSpuApi.getSpuList(
                convertSet(pageResult.getList(), QuickCombinationActivityDO::getSpuId));

        return success(QuickCombinationActivityConvert.INSTANCE.convertPage(pageResult, spus));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出进货活动 Excel")
    @PreAuthorize("@ss.hasPermission('promotion:quick-combination-activity:export')")
    @OperateLog(type = EXPORT)
    public void exportQuickCombinationActivityExcel(@Valid QuickCombinationActivityExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<QuickCombinationActivityDO> list = quickCombinationActivityService.getQuickCombinationActivityList(exportReqVO);
        // 导出 Excel
        List<QuickCombinationActivityExcelVO> datas = QuickCombinationActivityConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "进货活动.xls", "数据", QuickCombinationActivityExcelVO.class, datas);
    }

    @GetMapping("/record")
    @Operation(summary = "获得活动参与列表")
    @Parameter(name = "id", description = "活动编号", required = true, example = "1")
    public CommonResult<List<QuickCombinationRecordRespVO>> getQuickCombinationRecordList(@RequestParam("id") Long id){
        return success(quickCombinationRecordService.getRecorrd(id));
    }

}
